package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwan.manage.mapper.RedisDao;
import com.wenwan.manage.mapper.UserMapper;
import com.wenwan.manage.pojo.LoginToken;
import com.wenwan.manage.pojo.User;
import com.wenwan.manage.pojo.UserWeiXin;
import com.wenwan.manage.service.UserPhoneService;
import com.wenwan.manage.service.UserService;
import com.wenwan.manage.service.UserWeiXinService;
import com.wenwan.manage.setting.DefaultValue;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserWeiXinService userWeiXinService;

	private RedisDao redisDao = new RedisDao();

	@Override
	public Map<String, Object> createOrUpdate(String headimgurl, String nickname, String autograph, String zone,
			String openid) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (headimgurl != null && headimgurl.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "headimgurl" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("headimgurl" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (nickname != null && nickname.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "headimgurl" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("nickname" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (autograph != null && autograph.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "autograph" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("autograph" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (zone != null && zone.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "zone" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("zone" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		String id = Util.UUID();
		map.put("id", id);
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("headimgurl", headimgurl);
		map.put("nickname", nickname);
		map.put("autograph", autograph);
		map.put("zone", zone);
		map.put("releaseNumber", DefaultValue.USER_RELEASE_NUMBER);

		Map<String, Object> userWeiXin = userWeiXinService.selectByOpenid(openid);
		if (userWeiXin.get("data") == null) {
			Boolean data = userMapper.create(map);
			if (data) {
				data = (Boolean) userWeiXinService.create(openid, id).get("data");
			}
			// dto.setData(data);
			dto.put("data", data);
			dto.put("msg", StatusSetting.MSG_200);
			dto.put("code", StatusSetting.CODE_200);
			return dto;
		} else {
			Boolean data = (Boolean) this.update(((UserWeiXin) userWeiXin.get("data")).getId(), headimgurl, nickname,
					autograph, zone, null, null, null).get("data");
			// if (data) {
			// // data = userWeiXinService.create(openid, id).getData();
			// if (!data) {
			// dto.setCode(StatusSetting.CODE_10000);
			// dto.setMsg(StatusSetting.MSG_10000);
			// }
			// }
			// dto.setData(data);
			dto.put("data", data);
			dto.put("msg", StatusSetting.MSG_200);
			dto.put("code", StatusSetting.CODE_200);
			return dto;
		}

	}

	@Override
	public Map<String, Object> deleteById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		Boolean data = userMapper.deleteById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Autowired
	private UserPhoneService userPhoneService;

	@Override
	public Map<String, Object> update(String id, String headimgurl, String nickname, String autograph, String zone,
			String weiXin, Integer releaseNumber, String phone) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (headimgurl != null && headimgurl.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "headimgurl" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("headimgurl" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (nickname != null && nickname.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "nickname" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("nickname" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (autograph != null && autograph.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "autograph" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("autograph" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (zone != null && zone.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "zone" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("zone" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (weiXin != null && weiXin.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "weiXin" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("weiXin" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (phone != null && !phone.matches(RegExpSetting.PHONE)) {
			dto.put("data", false);
			dto.put("msg", "phone" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("phone" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("headimgurl", headimgurl);
		map.put("nickname", nickname);
		map.put("autograph", autograph);
		map.put("zone", zone);
		map.put("weiXin", weiXin);
		map.put("releaseNumber", releaseNumber);
		map.put("updateTime", Util.getTime());

		if (phone != null) {
			userPhoneService.create(phone, id);
		}

		Boolean data = userMapper.update(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize) {
		// TODO Auto-generated method stub
		// DataToObject<List<User>> dto = new DataToObject<List<User>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);

		List<User> data = userMapper.selectAll(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<User> dto = new DataToObject<User>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		User data = userMapper.selectOneById(map);
		String token = Util.UUID();

		LoginToken loginToken = new LoginToken();
		loginToken.setId(data.getId());
		loginToken.setToken(token);
		redisDao.putLoginToken(loginToken);

		// dto.setData(data);
		// dto.setToken(token);
		dto.put("data", data);
		dto.put("token", token);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneByOpenid(String openid) {
		// TODO Auto-generated method stub
		// DataToObject<User> dto = new DataToObject<User>();
		Map<String, Object> dto = new HashMap<String, Object>();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("openid", openid);

		User data = userMapper.selectOneByOpenid(map);
		String token = Util.UUID();

		LoginToken loginToken = new LoginToken();
		loginToken.setId(data.getId());
		loginToken.setToken(token);
		redisDao.putLoginToken(loginToken);

		// dto.setData(data);
		// dto.setToken(token);
		dto.put("data", data);
		dto.put("token", token);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
